package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelLabelMapper;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.apis.wemedia.IWeMediaClient;
import com.heima.model.admin.pojos.AdChannelLabel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.AdWmChannelDto;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AdChannelServiceImpl extends ServiceImpl<AdChannelLabelMapper, AdChannelLabel> implements AdChannelService {

    @Autowired
    private IWeMediaClient weMediaClient;
    @Autowired
    private AdChannelService adChannelService;

    /**
     * 查询频道列表
     * 调用远程Feign接口
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(AdWmChannelDto dto) {
    return   weMediaClient.list(dto);
    }

    /**
     * 保存频道
     * @param
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannelDto wmChannel) {
        if (wmChannel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ResponseResult responseResult = weMediaClient.saveChannel(wmChannel);
        if (responseResult.getCode()!=200){
            throw new RuntimeException("保存频道出错");
        }
        return responseResult;
    }

    @Override
    public ResponseResult updateChannel(WmChannelDto channel) {
        if (channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ResponseResult responseResult = weMediaClient.updateChannel(channel);
        if (responseResult.getCode()!=200){
            throw new RuntimeException("频道更新出错");
        }
        return responseResult;
    }

    @Override
    public ResponseResult delChannel(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannelLabel channelLabel = adChannelService.getById(id);
        if (channelLabel!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED);
        }
        weMediaClient.delChannel(id);
        log.info("删除频道成功");
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
